package com.yanxu.threadpool;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author 折戟沉沙铁未销
 * @version V1.0
 * @date 2025/4/13-2025
 * @Description: 线程池创建之后，在任务到来之前，就提前将核心线程全部创建启动起来。
 */
public class CoreThreadPreStart {
    //定义线程池
    public static ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
            2,
            4,
            60l,
            TimeUnit.SECONDS,
            new ArrayBlockingQueue<>(6));

    public static void main(String[] args) throws InterruptedException {
        //将 核心线程 提前启动
        //在任务到来之前，将核心线程全部创建启动起来
        threadPoolExecutor.prestartAllCoreThreads();

        for (int i = 0; i < 10; i++) {
            int finalI = i;
            threadPoolExecutor.submit(new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName()+" 任务"+ finalI +"运行完毕");
                }
            });
        }
    }
}